package com.example.myapplication.slice.slice.slice;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class userDataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    private RdbStore rdbStore;
    //RdbStore表示与数据库的连接，通过此对象可以完成对数据表中数据的CRUD操作
    //StoreConfig对象关联数据文件配置
    private StoreConfig config = StoreConfig.newDefaultConfig("UserStore.db");

    private RdbOpenCallback callback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            //使用rdbStore对象执行SQL创建数据表
            rdbStore.executeSql("create table if not exists users(scene integer primary key autoincrement,kind text not null,name text not null unique,location text)");
            rdbStore.executeSql("create table if not exists users(scene integer primary key autoincrement,kind text not null,name text not null unique,location text)");


        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "userDataAbility onStart");


        //初始化与数据库的连接
        DatabaseHelper helper = new DatabaseHelper(this);
        RdbStore rdbStore = helper.getRdbStore(config,1,callback);//数据库连接
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        //predicates封装查询操作
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, "location");
        ResultSet resultSet =  rdbStore.query(rdbPredicates,columns);
        //columns查询操作需要返回的类
        return resultSet;
    }//

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        int i = -1;
        //ValuesBucket valuesBucket = new ValuesBucket();
        //valuesBucket.putString("userName","zhangsan");
        //valuesBucket.putString("userTel","12345678910");
        //valuesBucket.putString("userAddr","北京");
        String path = uri.getLastPath();
        if ("location".equalsIgnoreCase(path)){
            i = (int)rdbStore.insert("location",value);
        }
        return i;


    }//实现用户信息的添加操作；uri表示DataAbility的访问路径;ValuesBucket传递添加数据的容器

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates,"location");
        int i = rdbStore.delete(rdbPredicates);
        return i;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates,"location");
        int i = rdbStore.update(value,rdbPredicates);
        return i;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}